package tool.codescan.mappers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import tool.csv.FileWriter;
import tool.data.TypeDefinition;

    /**
     * @author jwb09119
     * @date 2013/11/27
     * 
     * This class handles resolving Type names for implicit imports such as java, java.io and 
     * java.lang which are not declared, but are present in all Java classes.
     * 
     * There is probably a more elegant way to do this, but it is the fastest way for now and should
     * not need to be amended any time soon.  Also, I could not find a way to access the JRE in a
     * reasonable amount of time.
     * 
     * Also - looking in the compiles rt.jar zip, there are lots of things in there that are not
     * types.
     */

public class JavaImplicitImportMapper {
    
    Map<String, String> typeMap;
    
    public JavaImplicitImportMapper () {
    
        typeMap = new HashMap<>();
        setup();
        
    }
    
    
    /**
     * Retrieve a full type address for an implicit java import
     * @param type - the type to find a compete package address for
     * @return - TypeDefinition - if a path has been found, this information will be made available
     *           to the object passed in.
     * 
     */
    public TypeDefinition findImplicitImportType (TypeDefinition type) {
        
        if (typeMap.containsKey(type.getName())) {
            
            String packagePath = typeMap.get(type.getName());
            type.setPath(packagePath);
            
        }
        
        return type;
        
    }
    
    
    /**
     * Parses java import definition files.
     */
    private final void setup () {
        //FINAL - only final methods should be called from a constructor.
        char SLASH = File.separatorChar;
        
        File location = new File("src"+SLASH+"tool"+SLASH+"codescan"+SLASH+"mappers"+SLASH);
        
        File[] files = location.listFiles();
        
        for (int i = 0; i < files.length; i++) {
            
            if (files[i].isFile() && files[i].getName().endsWith(".implicit")) {
                
                parseImplicitImport(files[i]);
                
            }
            
        }
        
    }
    
    
    private void parseImplicitImport (File f) {
        
        String name = f.getName();
        name = name.substring(0, name.lastIndexOf("."));
     
        try (FileReader fis = new FileReader(f)) {
            
            parseSource(name, fis);
            
        } catch (FileNotFoundException e) {
            
        } catch (IOException e) {
            FileWriter.writeToLogFile("Problem reading java implicit import definition file: " + 
                                                      f.getAbsolutePath() + "\n" + e.getMessage());
            
        }
        
    }


    private void parseSource (String name, FileReader fr) throws IOException {

        BufferedReader br = new BufferedReader(fr);
        
        String line = null;
        line = br.readLine();
        
        while (line != null) {
           
            typeMap.put(line, name);
             line = br.readLine();
             
        }
        
    }
    
}
